import { FMVCS } from "../../fgui/fmvc/FMVCS";
import GuideCtrl from "./GuideCtrl";

export default class GuideView extends FMVCS.FView {
    updateTimer: number = 0;
    isTween: boolean = false;
    tweener: fgui.GTweener = null;
    clickHandlers() {
        return [
            // [this._views[`btnBlock`], this.onBtnBlock, this],
        ];
    }

    onOpenCallBack() {
        this.onFlush(`text`);
        this._views[`guideBlock`].visible = false;
        this.updateTimer = setTimeout(() => {
            this._views[`guideBlock`].visible = true;
            GuideCtrl.data.setGuide(GuideCtrl.data.curStep);
            this.onFlush();
        }, 200);
    }

    onFlush(type: string = `all`) {
        switch (type) {
            case `all`: {
                this._updateLblGuide();
                this._updateGuideBlock();
                break;
            }
            case `text`: {
                this._updateLblGuide();
            }
        }
    }

    private _updateLblGuide(){
        let lbl = this._views[`lblGuide`].asLabel;
        // if()
        lbl && (lbl.text = GuideCtrl.data.vo.text);
    }

    private _updateGuideBlock(){
        let vo = GuideCtrl.data.vo;
        let block = this._views[`guideBlock`];
        if(!block) return;
        if(block.x == vo.x && block.y == vo.y) return;
        if(this.isTween) return;
        this.isTween = true;
        this.tweener = fgui.GTween.to2(block.x, block.y, vo.x, vo.y, 0.5).setTarget(block, block.setPosition).onComplete(() => {
            this.isTween = false;
        }, this);
    }

    onClose(){
        super.onClose();
        if(this.isTween){
            this.tweener.kill(); 
            this.isTween = false;
        }
        clearTimeout(this.updateTimer);
    }
}